home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / e33el2.zip / emacs / 19.33 / lisp / resume.el < prev    next >
Lisp/Scheme  |  1996-05-23  |  5KB  |  129 lines

  1. ;;; resume.el --- process command line args from within a suspended Emacs job
  2.  
  3. ;; Copyright (C) 1992 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Joe Wells <jbw@bucsf.bu.edu>
  6. ;; Adapted-By: ESR
  7. ;; Keywords: processes
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ;; GNU General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  23. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. ;; Boston, MA 02111-1307, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; The purpose of this library is to handle command line arguments
  29. ;; when you resume an existing Emacs job.
  30.  
  31. ;; In order to use it, you must put this code in your .emacs file.
  32.  
  33. ;; (add-hook 'suspend-hook 'resume-suspend-hook)
  34. ;; (add-hook 'suspend-resume-hook 'resume-process-args)
  35.  
  36. ;; You can't get the benefit of this library by using the `emacs' command,
  37. ;; since that always starts a new Emacs job.  Instead you must use a
  38. ;; command called `edit' which knows how to resume an existing Emacs job
  39. ;; if you have one, or start a new Emacs job if you don't have one.
  40.  
  41. ;; To define the `edit' command, run the script etc/emacs.csh (if you use CSH),
  42. ;; or etc/emacs.bash if you use BASH.  You would normally do this in your
  43. ;; login script.
  44.  
  45. ;; Stephan Gildea suggested bug fix (gildea@bbn.com).
  46. ;; Ideas from Michael DeCorte and other people.
  47.  
  48. ;;; Code:
  49.  
  50. (defvar resume-emacs-args-file (expand-file-name "~/.emacs_args")
  51.   "*This file is where arguments are placed for a suspended emacs job.")
  52.  
  53. (defvar resume-emacs-args-buffer " *Command Line Args*"
  54.   "Buffer that is used by resume-process-args.")
  55.  
  56. (defun resume-process-args ()
  57.   "Handler for command line args given when Emacs is resumed."
  58.   (let ((start-buffer (current-buffer))
  59.     (args-buffer (get-buffer-create resume-emacs-args-buffer))
  60.     length args
  61.     (command-line-default-directory default-directory))
  62.     (unwind-protect
  63.     (progn
  64.       (set-buffer args-buffer)
  65.       (erase-buffer)
  66.       ;; get the contents of resume-emacs-args-file
  67.       (condition-case ()
  68.           (let ((result (insert-file-contents resume-emacs-args-file)))
  69.         (setq length (car (cdr result))))
  70.         ;; the file doesn't exist, ergo no arguments
  71.         (file-error
  72.           (erase-buffer)
  73.           (setq length 0)))
  74.       (if (<= length 0)
  75.           (setq args nil)
  76.         ;; get the arguments from the buffer
  77.         (goto-char (point-min))
  78.         (while (not (eobp))
  79.           (skip-chars-forward " \t\n")
  80.           (let ((begin (point)))
  81.         (skip-chars-forward "^ \t\n")
  82.         (setq args (cons (buffer-substring begin (point)) args)))
  83.           (skip-chars-forward " \t\n"))
  84.         ;; arguments are now in reverse order
  85.         (setq args (nreverse args))
  86.         ;; make sure they're not read again
  87.         (erase-buffer))        
  88.       (resume-write-buffer-to-file (current-buffer) resume-emacs-args-file)
  89.       ;; if nothing was in buffer, args will be null
  90.       (or (null args)
  91.           (setq command-line-default-directory
  92.             (file-name-as-directory (car args))
  93.             args (cdr args)))
  94.       ;; actually process the arguments
  95.       (command-line-1 args))
  96.       ;; If the command line args don't result in a find-file, the
  97.       ;; buffer will be left in args-buffer.  So we change back to the
  98.       ;; original buffer.  The reason I don't just use
  99.       ;; (let ((default-directory foo))
  100.       ;;    (command-line-1 args))
  101.       ;; in the context of the original buffer is because let does not
  102.       ;; work properly with buffer-local variables.
  103.       (if (eq (current-buffer) args-buffer)
  104.       (set-buffer start-buffer)))))
  105.  
  106. ;;;###autoload
  107. (defun resume-suspend-hook ()
  108.   "Clear out the file used for transmitting args when Emacs resumes."
  109.   (save-excursion
  110.     (set-buffer (get-buffer-create resume-emacs-args-buffer))
  111.     (erase-buffer)
  112.     (resume-write-buffer-to-file (current-buffer) resume-emacs-args-file)))
  113.  
  114. (defun resume-write-buffer-to-file (buffer file)
  115.   "Writes the contents of BUFFER into FILE, if permissions allow."
  116.   (if (not (file-writable-p file))
  117.       (error "No permission to write file %s" file))
  118.   (save-excursion
  119.     (set-buffer buffer)
  120.     (clear-visited-file-modtime)
  121.     (save-restriction
  122.       (widen)
  123.       (write-region (point-min) (point-max) file nil 'quiet))
  124.     (set-buffer-modified-p nil)))
  125.  
  126. (provide 'resume)
  127.  
  128. ;;; resume.el ends here
  129.